package br.com.avaliacao.utils;

import br.com.avaliacao.model.entities.Avaliacao;
import br.com.avaliacao.model.entities.Colaborador;
import br.com.avaliacao.model.entities.Criterio;
import br.com.avaliacao.model.entities.ItemAvaliacao;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {

    private static SessionFactory sessao;

    private HibernateUtil() {
    }

    public static SessionFactory getSessao() {

        if (sessao == null) {
            try {
                Configuration configuration = new Configuration();
                configuration.configure();

                configuration.addAnnotatedClass(Avaliacao.class);
                configuration.addAnnotatedClass(Colaborador.class);
                configuration.addAnnotatedClass(Criterio.class);
                configuration.addAnnotatedClass(ItemAvaliacao.class);

                ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
                sessao = configuration.buildSessionFactory(serviceRegistry);

            } catch (Throwable ex) {
                throw new ExceptionInInitializerError();
            }
        }
        
        return sessao;
    }
}
